// pages/group-head/waite-take-order.js
const app = getApp()
const orderActions = [{
    name: "确认配送",
    method: "doOrder",
    type: 1
},
{
    name: "确认到货",
    method: "doOrder",
    type: 2
},
{
    name: "扫码提货",
    method: "scanCode",
    type: 3
},
{
    name: "取件码提货",
    method: "showPutCode",
    type: 3
},
]

function findAction() {
    return orderActions.filter(o => {
        return Array.from(arguments).indexOf(o.name) >= 0
    })
}

function getOrderAction(item) {
    const status = parseInt(item.status)
    let r = []
    switch (status) {
        case 20:
            r = findAction("确认配送")
            break
        case 21:
            r = findAction("确认到货")
            break
        case 11:
            r = findAction("扫码提货", "取件码提货")
            break
        case 40:
            break
    }
    return r
}

Page({

    data: {
        status: 11
    },

    onLoad: function(options) {
        this.loadData()
    },
    loadData() {
        let {
            page = 1, nextPage = true, loading = false, list = []
        } = this.data
        if (loading || !nextPage) return
        this.setData({
            loading: true
        })
        wx.showLoading({
            title: '加载中...',
        })
        app.http.post('administrator/order-list?page=' + page, {
            data: {
                status: this.data.status,
                type: 0
            }
        }).then(res => {
            page++
            nextPage = res.data.pageStatus
            list.push(...res.data.list.map(o => {
                o.actions = getOrderAction(o)
                return o
            }))
            loading = false
            this.setData({
                page,
                nextPage,
                list,
                loading
            })
            wx.hideLoading()
        })
    },
    //操作按钮
    doOrder(evt) {
        let {
            submittype,
            orderid
        } = evt.currentTarget.dataset
        wx.showLoading({
            title: '请稍等...',
        })
        let data = {
            order_id: orderid,
            type: submittype
        }
        if (this.data.take_code) {
            this.setData({
                showPutCode: false
            })
            data.take_code = this.data.take_code
        }
        app.http.get('administrator/status', {
            data
        }).then(res => {
            wx.hideLoading()
            if (res.data.status == 1) {
                wx.showToast({
                    icon: 'none',
                    title: '操作成功',
                })
                this.onPullDownRefresh()
            } else {
                wx.showModal({
                    title: '小提示',
                    content: res.data.msg,
                    showCancel: false,
                    confirmColor: '#ff5400'
                })
            }
        })

    },
    //扫二维码
    scanCode(evt) {
        let {
            submittype,
            orderid
        } = evt.currentTarget.dataset
        wx.scanCode({
            success: res => {
                this.setData({
                    take_code: res.result,
                })
                this.doOrder({
                    currentTarget: {
                        dataset: {
                            submittype,
                            orderid
                        }
                    }
                })
            }
        })
    },
    //弹出取件码输入框
    showPutCode(evt) {
        let {
            submittype,
            orderid
        } = evt.currentTarget.dataset
        let obj = {
            showPutCode: !this.data.showPutCode,
        }
        if (submittype && orderid) {
            obj.submittype = submittype
            obj.orderid = orderid
        }
        this.setData(obj)
    },
    //取件码取件
    putCodeSubmit(evt) {
        console.log(evt, 'xxxxxxxxxx')
        let take_code = evt.detail.value.take_code
        this.setData({
            take_code
        })
        this.doOrder({
            currentTarget: {
                dataset: {
                    submittype: this.data.submittype,
                    orderid: this.data.orderid
                }
            }
        })

    },
    //手机号搜索
    showInput() {
        this.setData({
            showInput: !this.data.showInput
        })
    },

    // 搜索
    mobileSearch(evt) {
        app.http.post("administrator/order-list", {
            data: {
                status: this.data.status,
                type: 0,
                mobile: evt.detail.value
            }
        }).then(res => {
            this.setData({
                list: res.data.list.map(o => {
                    o.actions = getOrderAction(o)
                    return o
                })
            })
        })
    },
    onPullDownRefresh: function () {
        this.setData({
            loading:false,
            nextPage:true,
            list:[],
            page:1
        })
        this.loadData()
    },

    onReachBottom: function () {
        this.loadData()
    },
})